/*
 * Copyright (C) 2021 The Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.oushangfeng.pinneddemo.spinner;

import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;

/**
 * Utils
 *
 * @since 2021-03-27
 */
final class Utils {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "Utils");
    private static final int NUM_16 = 16;
    private static final int NUM_255 = 255;
    private static final int NUM_FF = 0xFF;
    private static final int NUM_8 = 8;
    private static final int NUM_0 = 0;

    static int darker(int color, float factor) {
        return Color.argb(Color.alpha(color), Math.max((int) (Integer.parseInt(red(color)) * factor), 0),
                Math.max((int) (Integer.parseInt(green(color)) * factor), 0),
                Math.max((int) (Integer.parseInt(blue(color)) * factor), 0));
    }

    static int lighter(int color, double factor) {
        double red = (Integer.parseInt(red(color), NUM_16) * (1 - factor) / NUM_255 + factor) * NUM_255;
        double green = ((double)Integer.parseInt(green(color), NUM_16)   / NUM_255 + factor) * NUM_255;
        double blue = (Integer.parseInt(blue(color), NUM_16) * (1 - factor) / NUM_255 + factor) * NUM_255;
        return Color.argb(Color.alpha(color), (int) red, (int) green, (int) blue);
    }

    static String red(int color) {
        String toHexString = Integer.toHexString((color >> NUM_16) & NUM_FF);
        if (toHexString.length() == 1) {
            toHexString = "" + NUM_0 + toHexString;
        }
        return toHexString;
    }

    static String green(int color) {
        String toHexString = Integer.toHexString((color >> NUM_8) & NUM_FF);
        if (toHexString.length() == 1) {
            toHexString = "" + NUM_0 + toHexString;
        }
        return toHexString;
    }

    static String blue(int color) {
        String toHexString = Integer.toHexString(color & NUM_FF);
        if (toHexString.length() == 1) {
            toHexString = "" + NUM_0 + toHexString;
        }
        return toHexString;
    }

    static boolean isRtl(Context context) {
        return context.getResourceManager().getConfiguration().isLayoutRTL;
    }

    static PixelMapElement getPixelMapElement(Context context, int drawableId) {
        PixelMap pixelMap = getPixelMap(context, drawableId);
        if (pixelMap == null) {
            return (PixelMapElement) Collections.emptyList();
        }
        return new PixelMapElement(pixelMap);
    }

    static PixelMap getPixelMap(Context context, int drawableId) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = context.getResourceManager().getResource(drawableId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/png";
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (IOException | NotExistException e) {
            HiLog.error(LABEL, "IOException|NotExistException");
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (IOException e) {
                HiLog.error(LABEL, "IOException");
            }
        }
        return (PixelMap) Collections.emptyList();
    }

    static boolean isEmpty(CharSequence str) {
        return str == null || str.length() == 0;
    }
}
